export function uniGetStorageSync(key, reset = false) {
	return uni.getStorageSync(key);
}

export function uniSetStorageSync(key, val) {
	uni.setStorageSync(key, val);
}

export function getCurrentUserProfile() {
	if (didntLoggedIn(false)) return;
	return new Promise((resolve) => {
		if (USER_ORIENTED) {
			tokenRequest(
				'/user_info', {}, {
					success: ({
						data
					}) => {
						if (data.code === 200) {
							uniSetStorageSync(CURRENT_USER_KEY, data.data);
							resolve(data.data);
						}
					},
				}
			);
		} else {}
	});
}

export function didntLoggedIn(showToast = true, toRedirect = true) {
	let token = uniGetStorageSync(TOKEN_key);
	if (!token) {
		if (!USER_ORIENTED && toRedirect) {
			// 如果是招商端, 需要你一直登录
			uniRedirect('/login');
		}
		if (showToast) {
			uniShowToast('请先登录', false);
		}
		return true;
	}
	return false;
}

export function uniChooseImage(opt) {
	uni.chooseImage({
		count: 1,
		sizeType: ['compressed'],
		sourceType: ['album', 'camera'],
		success: (res) => {
			// console.log(res)
		},
		fail: (err) => {
			// console.log('chooseImage fail', err)
			alert('图片')
			// #ifdef APP-PLUS || H5
			uni.getSetting({
				success: (res) => {
					let authStatus = res.authSetting['scope.album'];
					if (!authStatus) {
						uni.showModal({
							title: '授权失败',
							content: 'Hello uni-app需要从您的相册获取图片，请在设置界面打开相关权限',
							success: (res) => {
								if (res.confirm) {
									uni.openSetting();
								}
							},
						});
					}
				},
			});
			// #endif
		},
		...opt,
	});
}